import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:management/model/gate_model.dart';

///License Plate Recognition 车牌识别相机
class AddLPRCamera extends StatefulWidget {
  @override
  _AddLPRCameraState createState() => _AddLPRCameraState();
}

class _AddLPRCameraState extends State<AddLPRCamera> {
  TextEditingController _deviceNameController = TextEditingController();
  TextEditingController _deviceIDController = TextEditingController();

  // TextEditingController _siteIDController = TextEditingController();
  TextEditingController _macController = TextEditingController();

  _buildBody() => SingleChildScrollView(
        child: Container(
          // height: 500,
          padding: EdgeInsets.all(20),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Container(
                height: 60,
                child: Row(
                  children: [
                    Text(
                      "设备名称:",
                      style:
                          TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
                    ),
                    Container(
                      margin: EdgeInsets.only(left: 25),
                      width: 300,
                      child: TextField(
                        controller: _deviceNameController,
                        style: TextStyle(fontSize: 22),
                        decoration: InputDecoration(
                          hintText: "请输入要添加的设备名称",
                          border: OutlineInputBorder(),
                        ),
                      ),
                    )
                  ],
                ),
              ),
              // Container(
              //   height: 60,
              //   margin: EdgeInsets.only(top: 50),
              //   child: Row(
              //     children: [
              //       Text(
              //         "场  地  ID:",
              //         style: TextStyle(
              //           fontSize: 25,
              //           fontWeight: FontWeight.bold,
              //         ),
              //       ),
              //       Container(
              //         width: 300,
              //         margin: EdgeInsets.only(left: 25),
              //         child: TextField(
              //           controller: _siteIDController,
              //           style: TextStyle(fontSize: 22),
              //           decoration: InputDecoration(
              //             hintText: "请输入场地ID",
              //             border: OutlineInputBorder(),
              //           ),
              //         ),
              //       ),
              //     ],
              //   ),
              // ),
              Container(
                margin: EdgeInsets.only(top: 50),
                child: Row(
                  children: [
                    Text(
                      "设  备  ID:",
                      style:
                          TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
                    ),
                    Container(
                      margin: EdgeInsets.only(left: 25),
                      width: 300,
                      child: TextField(
                        controller: _deviceIDController,
                        style: TextStyle(fontSize: 22),
                        decoration: InputDecoration(
                          hintText: "请输入要添加的设备ID",
                          border: OutlineInputBorder(),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: 50),
                child: Row(
                  children: [
                    Text(
                      "设备MAC:",
                      style:
                          TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
                    ),
                    Container(
                      margin: EdgeInsets.only(left: 20),
                      width: 300,
                      child: TextField(
                        controller: _macController,
                        style: TextStyle(fontSize: 22),
                        decoration: InputDecoration(
                          hintText: "请输入设备mac地址",
                          border: OutlineInputBorder(),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              Container(
                // color: Colors.grey,
                height: 50,
                margin: EdgeInsets.only(top: 50),
                width: MediaQuery.of(context).size.width,
                child: OutlinedButton(
                  child: Text(
                    "确定",
                    style: TextStyle(fontSize: 30),
                  ),
                  onPressed: () {
                    insertData();
                  },
                ),
              ),
            ],
          ),
        ),
      );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _buildBody(),
    );
  }

  void insertData() async {
    String name = _deviceNameController.text;
    String deviceID = _deviceIDController.text;
    // String siteID = _siteIDController.text;
    String mac = _macController.text;
    if (TextUtil.isEmpty(name) ||
        TextUtil.isEmpty(deviceID) ||
        // TextUtil.isEmpty(siteID) ||
        TextUtil.isEmpty(mac)) {
      Fluttertoast.showToast(msg: "数据不能为空");
      return;
    }
    var gateModel = await GateProvider.instance.queryByName(name);
    if (gateModel != null) {
      Fluttertoast.showToast(msg: "设备名称已存在");
      return;
    }

    // SpUtil.putString("siteID", siteID);
    GateProvider.instance.insert(GateModel(name, deviceID, "0", mac));
    Navigator.of(context).pop();
  }
}
